<?php

//18
session_start();
require 'connection.php';

$app = new \Slim\Slim();
$app->get('/header/find_all/:type/:page', authorize('admin'), 'findAll');
//         /art_report/categor
$app->get('/header/search/:search', authorize('admin'), 'search');
$app->get('/header/:id', authorize('admin'), 'read');
$app->post('/header', authorize('admin'), 'add');
$app->put('/header/:id', authorize('admin'), 'edit');
$app->delete('/header/:id', authorize('admin'), 'delete');

$app->run();

function findAll($type, $page) {
    $page = ($page - 1) * 10;
    $sql_list = "SELECT id.id, id.number, id.serie, id.date, id.operator_id, id.total, id.subtotal, id.igv, id.type, id.operator_type,
id.document_id, id.type_change, io.name, io.code, io.address, itc.value
FROM import_document id
INNER JOIN operators io
ON id.operator_id = io.id
LEFT  JOIN import_type_change itc
ON id.type_change = itc.id WHERE id.type = " . $type . " AND id.period = " . $_SESSION['current_month'] . " AND id.year = " . $_SESSION['current_year'] . " LIMIT " . $page . ", 10";
    $sql_count = "SELECT count(*) FROM import_document WHERE type = " . $type . " AND period = " . $_SESSION['current_month'] . " AND year = " . $_SESSION['current_year'] . "";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_list);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $result = $db->prepare($sql_count);
        $result->execute();
        $rst_count = $result->fetchColumn();
        $db = null;
        echo '{"result":' . json_encode($rst_find) . ', "count" :' . $rst_count . ' }';
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}

function search($search) {
    $sql_list = "SELECT * FROM categories WHERE name LIKE '%" . $search . "%' ";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_list);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo '{"result":' . json_encode($rst_find) . ' , "count" :0}';
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}

function read($id) {
    $sql_read = "SELECT * FROM import_document WHERE id = :id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_read);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $rst = $stmt->fetchObject();
        $db = null;
        echo json_encode($rst);
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
    }
}

// document_id, type_change, operator_type, period, year
function add() {
    $sql_add_header = "INSERT INTO import_document(type, serie, number, date, operator_id,total,igv,subtotal,document_id, type_change, operator_type, period, year, money_type) 
        VALUES (:type, :serie, :number, :date, :operator_id,:total,:tax,:subtotal,:document_id, :type_change, :operator_type, :period, :year, :money_type)";

    $request = \Slim\Slim::getInstance()->request();
    $param = json_decode($request->getBody());
    try {
        $db = getConnection();
        $db->beginTransaction();
        $stmt = $db->prepare($sql_add_header);
        $stmt->bindParam("serie", $param->serie);
        $stmt->bindParam("number", $param->number);
        $stmt->bindParam("date", $param->date);
        $stmt->bindParam("type", $param->type);
        $stmt->bindParam("operator_id", $param->operator_id);
        $stmt->bindParam("total", $param->total);
        $stmt->bindParam("tax", $param->tax);
        $stmt->bindParam("subtotal", $param->subtotal);
        $stmt->bindParam("document_id", $param->document_id);
        $stmt->bindParam("type_change", $param->type_change);
        $stmt->bindParam("operator_type", $param->operator_type);
        $stmt->bindParam("money_type", $param->money_type);
        $stmt->bindParam("period", $_SESSION['current_month']);
        $stmt->bindParam("year", $_SESSION['current_year']);
        $stmt->execute();

        $db->commit();
    } catch (PDOException $e) {
        $db->rollBack();
        echo '{"error":{"text":' . $e->getMessage() . '}}';
    }
    $db = null;
}

function edit($id) {

    $sql_edit = "UPDATE import_document SET 
        type = :type, 
        serie = :serie, 
        number = :number, 
        date = :date, 
        operator_id = :operator_id,
        total = :total,
        igv = :tax,
        subtotal = :subtotal,
        document_id = :document_id, 
        type_change = :type_change, 
        operator_type = :operator_type, 
        period = :period, 
        year = :year
        WHERE id=:id";

    $request = \Slim\Slim::getInstance()->request();
    $body = $request->getBody();
    $param = json_decode($body);
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_edit);
        $stmt->bindParam("serie", $param->serie);
        $stmt->bindParam("number", $param->number);
        $stmt->bindParam("date", $param->date);
        $stmt->bindParam("type", $param->type);
        $stmt->bindParam("operator_id", $param->operator_id);
        $stmt->bindParam("total", $param->total);
        $stmt->bindParam("tax", $param->tax);
        $stmt->bindParam("subtotal", $param->subtotal);
        $stmt->bindParam("document_id", $param->document_id);
        $stmt->bindParam("type_change", $param->type_change);
        $stmt->bindParam("operator_type", $param->operator_type);
        $stmt->bindParam("period", $_SESSION['current_month']);
        $stmt->bindParam("year", $_SESSION['current_year']);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $db = null;
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
    }
}

function delete($id) {
    $sql_delete = "DELETE FROM import_document WHERE id=:id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_delete);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $db = null;
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
    }
}

?>